package com.chengqs.rainbow.im.client.retry;

import com.chengqs.rainbow.common.protobuf.Command;
import com.chengqs.rainbow.im.client.listener.CommandListener;
import com.sun.istack.internal.NotNull;
import lombok.Getter;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * <B>主类名称：</B>DelayTask<BR>
 * <B>概要说明：</B>enter<BR>
 *
 * @author: chengqs
 * @create: 2025-09-14 20:15:53
 **/
@Getter
public class DelayTask<T> implements Delayed {

    private final CommandListener failedListener;

    private final Command command;

    private final T data;

    private final long expireTime;

    private int count = 0;

    public DelayTask(Command command, T data, long expireTime, CommandListener failedListener) {
        this.command = command;
        this.data = data;
        this.expireTime = System.currentTimeMillis() + expireTime;
        this.failedListener = failedListener;
    }

    @Override
    public long getDelay(@NotNull TimeUnit unit) {
        return expireTime - System.currentTimeMillis();
    }

    @Override
    public int compareTo(@NotNull Delayed o) {
        return (int) (getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }

    public void retry() {
        count++;
    }
}
